<?php
namespace app\index\controller;


use app\admin\util\Response;
use app\index\model\ProgramCommentModel;
use app\index\model\ProgramLikeModel;
use app\index\model\ProgramModel;
use app\index\util\Page;
use app\index\validate\ProgramValidate;
use think\Config;
use think\Db;
use think\Request;
use HTMLPurifier_Config;
use HTMLPurifier;
class Program extends BaseMessage
{
    public function __construct(Request $request = null)
    {
        $this->_model = new ProgramModel();
        $this->_validate = new ProgramValidate();
        $this->_like_model = new ProgramLikeModel();
        $this->_comment_model = new ProgramCommentModel();
        $this->_template_add = 'add';
        $this->_template_detail = 'detail';
        $this->_template_index = 'index';
        $this->_template_ajax_list = 'ajax_list';
        parent::__construct($request);
    }
    protected function _before_add()
    {
        $this->assign('action',url('insert'));
        $id = input('id',0,'intval');
        $this->assign('back_url',url('index'));
        if($id != 0){
            $where = [];
            $where['id'] = $id;
            $where['uid'] = $this->_user_id;
            $vo = $this->_model->where($where)->find();
            if($vo){
                $vo['detail'] = str_replace(array("\r\n", "\r", "\n"), "", $vo['detail']);
                $vo['detail'] = str_replace('"', '\"', $vo['detail']);
                $this->assign('vo',$vo);
                $this->assign('action',url('update'));
            }
            $this->assign('back_url',url('my_list'));
        }
    }

    public function insert(){
        $param = [];
        $param['title'] = input('title','','htmlspecialchars');
        $param['detail'] = input('detail','','htmlspecialchars');
        $config = HTMLPurifier_Config::createDefault();
        $purifier = new HTMLPurifier($config);
        $param['detail'] = $purifier->purify($param['detail']);
        $param['uid'] = $this->_user_id;
        if(Config::get('site.message_default_status') == 1){
            $param['status'] = 1;
        }else{
            $param['status'] = 0;
        }
        $result = $this->_validate->check($param);
        if($result === false){
            Response::json(-1,$this->_validate->getError());
        }else{
            $result = $this->_model->isUpdate(false)->save($param);
            if($result){
                Response::json(1,'操作成功');
            }else{
                Response::json(-1,'添加失败');
            }
        }
    }
    public function update(){
        $param = [];
        $param['title'] = input('title','','htmlspecialchars');
        $param['detail'] = input('detail','','htmlspecialchars');
        $param['id'] = input('id','','intval');
        $config = HTMLPurifier_Config::createDefault();
        $purifier = new HTMLPurifier($config);
        $param['detail'] = $purifier->purify($param['detail']);
        $result = $this->_validate->check($param);
        if($result === false){
            Response::json(-1,$this->_validate->getError());
        }else{
            $result = $this->_model->isUpdate(true)->save($param,['id'=>$param['id']]);
            if($result){
                Response::json(1,'更新成功');
            }else{
                Response::json(-1,'更新失败');
            }
        }
    }
    /**
     * 输出列表页
     * @return \think\response\View
     */
    public function index()
    {
        $order = input('order',1,'intval');
        $list = $this->_get_list($order,$this->_size,1);
        if($list['total'] <= $this->_size){
            $this->assign('list_end',1);
        }else{
            $this->assign('list_end',0);
        }
        $this->assign('list',$list['data']);
        $this->assign('order',$order);
        return view($this->_template_index);
    }

    /**
     * 获取列表方式
     * @param $order
     * @param $size
     * @param $page
     * @return array
     */
    protected function _get_list($order,$size,$page){
        if($order == 1){
            $list = $this->_model->field('*')->alias('m')->join('(SELECT count(id) as like_num,message_id FROM '.$this->_like_model->getTable().' GROUP BY message_id) AS c',' m.id = c.message_id','LEFT')->where(['m.status' => 1])->order('like_num desc')->select();
        }else if($order == 2){
            $sql = 'SELECT * FROM '.$this->_model->getTable().' WHERE status = 1 ORDER BY add_time desc';
            $list = Db::query($sql);
        }elseif($order == 3){
            $sql = 'SELECT * FROM '.$this->_model->getTable().' WHERE status = 1 ORDER BY pv desc';
            $list = Db::query($sql);
        }
        return Page::return_page_data($list,$size,$page);
    }
}
